﻿using System;
using System.Collections.Generic;
using System.Text;
using MultithreadingWorker;
using MortgageMonitor.DDL;
using Mortgage.DAL;
using Mortgage.DDL;
using StockTrader.BusinessService.DataContract.Mortgate;
using LoanMonitor.Business;
using Mortgage.Enum;

namespace Mortgage.Parallel
{
    public delegate void UserInfoCompletedEventHandler(object sender, UserInfoEventArgs e);

    public class GenerateAccountInfo : ThreadWrapperBase
    {
        public event UserInfoCompletedEventHandler completed;
        string AccountNo;
        bool isDiscount = false;
        bool isCountSettlement = false;
        bool isIntraday = false;

        AccountInfoUI AccInfo;

        public GenerateAccountInfo(string accountno, bool _isDiscount, bool _isCountSettlement, bool _isIntraday)
        {
            AccountNo = accountno;
            isDiscount = _isDiscount;
            isCountSettlement = _isCountSettlement;
            isIntraday = _isIntraday;
            SupportsProgress = true;
        }

        protected override void DoTask()
        {
            try
            {
                progress = 30;
                AccountInfoUI negative = StaticDataAccess.GetAccountInfoUI(AccountNo, true, true, isIntraday);
                //AccountInfoUI original = StaticDataAccess.GetAccountInfoUI(AccountNo, false, true, isIntraday);
                AccountInfoUI positive = StaticDataAccess.GetAccountInfoUI(AccountNo, false, true, isIntraday);
                AccInfo = new AccountInfoUI(new AccountUI(negative.Identity.Name, negative.Identity.Gender, AccountNo, negative.Identity.IDcardNo, negative.Identity.IDcardDate, negative.Identity.IDcardIssuer, negative.Identity.PhoneMobile, negative.Identity.PhoneFix, negative.Identity.Email, negative.Identity.AddressContact, negative.Identity.AddressResident),
                    new CashHoldingUI(positive.CashHolding.CashTrading,
                        positive.CashHolding.CashWaiting, positive.CashHolding.CashBlocking,
                        positive.CashHolding.PreLoan, positive.CashHolding.AccumulateLoanValue,
                        positive.CashHolding.StockTradingValue, positive.CashHolding.StockWaitingValue,
                        negative.CashHolding.StockTradingValue, negative.CashHolding.StockWaitingValue),
                        positive.StocksHolding, positive.OpenContracts, positive.ClosedContracts);
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        protected override void OnCompleted()
        {
            if (completed != null)
                completed(this, new UserInfoEventArgs(AccountNo, isDiscount, isCountSettlement, isIntraday));
        }

        public AccountInfoUI GetResultOfLastTask()
        {
            if (Status == StatusState.Completed)
                return AccInfo;
            else
                return null;
        }



    }

    public class UserInfoEventArgs : EventArgs
    {
        public string Account { set; get; }
        public bool IsDiscount { set; get; }
        public bool IsCountSettlement { set; get; }
        public bool IsIntraday { set; get; }

        public UserInfoEventArgs(string accountno, bool _isDiscount, bool _isCountSettlement, bool _isIntraday)
        {
            Account = accountno;
            IsDiscount = _isDiscount;
            IsCountSettlement = _isCountSettlement;
            IsIntraday = _isIntraday;
        }
    }
}
